# 导入相关库
import numpy as np  # 导入numpy库，用于处理数组和数值计算
import math
from jili.tool.convert import data_dropna
import matplotlib.pyplot as plt  # 导入matplotlib的绘图模块，用于可视化
plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题


def chart_line(boxs,url=None):
    boxs=data_dropna(boxs)
    plt.figure(figsize=(16, 9), dpi=100)
    # plt.subplot(2, 2, 1)
    for k,v in boxs.items():
        plt.plot(v,label=k)
    plt.title('时序图')
    plt.legend()
    if url is None:
        # 显示图表
        plt.show()
    else:
        plt.savefig(url, bbox_inches='tight', pad_inches=0.01, dpi=100)
        plt.close()
    return url
def chart_hist(boxs,url=None):
    boxs = data_dropna(boxs)
    plt.figure(figsize=(16, 9), dpi=100)
    for k,box1 in boxs.items():
        bins = int(len(box1) / 10)
        if bins > 100:
            bins = 100
        box1.hist(bins=bins, label=k, alpha=0.7)
    plt.vlines(0, ls='--', alpha=0.5, label='0')
    plt.title('频率分布')
    plt.legend()
    if url is None:
        # 显示图表
        plt.show()
    else:
        plt.savefig(url, bbox_inches='tight', pad_inches=0.01, dpi=100)
        plt.close()
    return url
def chart_boxplot(boxs,url=None):
    boxs = data_dropna(boxs)
    plt.figure(figsize=(16, 9), dpi=100)
    plt.boxplot(list(boxs.values()), vert=False,labels=list(boxs.keys()))
    n=len(boxs)
    plt.vlines(0,ymin=0, ymax=n, ls='--',alpha=0.5, label='0')
    plt.title('箱型图')
    plt.legend()
    if url is None:
        # 显示图表
        plt.show()
    else:
        plt.savefig(url, bbox_inches='tight', pad_inches=0.01, dpi=100)
        plt.close()
    return url